/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.chl.niclun.projectwebshop.nbp.Entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.validation.constraints.NotNull;

/**
 *
 * @author niclas
 */
@Entity
public class Actor implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @NotNull
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long actorId;
    
    @NotNull
    @Column(name="FirstName")
    private String firstName;
    @NotNull
    @Column(name="LastName")
    private String lastName;
    
    @JoinColumn(name="Movies")
    @ManyToMany(cascade = CascadeType.ALL, mappedBy="actors")
    private List<Movie> movies;
    
    //private String picture;
    
    public Actor() {}
    
    public Actor(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
        movies = new ArrayList<Movie>();
    }
    
    public Long getActorId() {
        return actorId;
    }

    public void setActorId(Long id) {
        this.actorId = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public List<Movie> getMovies() {
        return movies;
    }

    public void setMovies(List<Movie> movies) {
        this.movies = movies;
    }
    
    public void addMovie(Movie movie) {
            movies.add(movie);
    }
    
    public void removeMovie(Movie movie) {
        Iterator<Movie> it = movies.iterator();
        Movie m;
        while(it.hasNext()) {
            m = it.next();
            if(m.getMovieId().equals(movie.getMovieId()))
                it.remove();
        }
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (actorId != null ? actorId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Actor)) {
            return false;
        }
        Actor other = (Actor) object;
        if ((this.actorId == null && other.actorId != null) || (this.actorId != null && !this.actorId.equals(other.actorId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "edu.chl.niclun.projectwebshop.nbp.Actor[ id=" + actorId + " ]";
    }
    
}
